//
//  SSFirstPageTravelView.m
//  Travel_World
//
//  Created by dlios on 15/10/14.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "SSFirstPageTravelView.h"
#import "SSFirstPageTravelCityModel.h"
#import "QTAFNnetWorkTool.h"
#import "UIImageView+WebCache.h"
#import "SSFirstPageTravelCityCell.h"
#import "SSFirstPageTravelHotCityCell.h"
#import "SSFirstPageBookingHotelView.h"
#import "MRCityViewController.h"
#import "Masonry.h"

@interface SSFirstPageTravelView ()<UITableViewDataSource, UITableViewDelegate, SSFirstPageTravelHotCityCellDelegate>

@property (nonatomic, copy) NSString *myURL;
@property (nonatomic, strong) NSMutableArray *travelCityArr;
@property (nonatomic, strong) NSMutableArray *titleOfSectionArr;
@property (nonatomic, strong) NSMutableArray *cityOfSectionArr;
@property (nonatomic, strong) UITableView *maintableView;
@property (nonatomic, strong) UIView *navigationView;

@end

@implementation SSFirstPageTravelView


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.myURL = @"http://open.qyer.com/qyer/hotel/hot_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=199&page=1&track_app_channel=App%2520Store&track_app_version=6.6&track_device_info=iPad%2520Air%2520%28Wifi%29&track_deviceid=A24310D6-4784-474E-86CE-81D543A1EB8A&track_os=ios%25208.1.3&v=1";
    }
    
    return self;
}


- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.layer.masksToBounds = NO;
    self.navigationController.navigationBar.translucent = NO;
    //self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.949 green:0.179 blue:1.000 alpha:1.000];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"旅途中";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:239 / 255.0 green:238 / 255.0 blue:230 / 255.0 alpha:0.491];
    
    [self createMainTableView];
    [self createData];
    [self createLiftButton];
    
}

- (void)createLiftButton
{
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonClick)];
    //leftBarButton.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}


- (void)leftBarButtonClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)createMainTableView
{
    self.maintableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.maintableView];
    self.maintableView.delegate = self;
    self.maintableView.dataSource = self;
    
    [self.maintableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.and.top.right.bottom.equalTo(self.view);
        
    }];
}


- (void)createData
{
    [QTAFNnetWorkTool getUrl:self.myURL body:nil hudSuperView:self.view result:QTJSON headerFile:nil success:^(id result) {
        
        self.travelCityArr = [SSFirstPageTravelCityModel baseModelByArr:result[@"data"]];
        
        [self createArr];
        
    }];
}

- (void)createArr
{
    self.titleOfSectionArr = [SSFirstPageTravelCityModel titleForSectionByArr:self.travelCityArr];
    
    self.cityOfSectionArr = [SSFirstPageTravelCityModel cityForSectionByArr:self.travelCityArr];
    
    [self.maintableView reloadData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleOfSectionArr.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else {
        return [self.cityOfSectionArr[section] count];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        return WIDTH > MINW ? MINW * 0.6 : MINW * 0.8;
    } else {
        return 80;
    }
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.titleOfSectionArr[section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.titleOfSectionArr;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        static NSString *city = @"Hotcity";
        SSFirstPageTravelHotCityCell *cell = [tableView dequeueReusableCellWithIdentifier:city];
        if (!cell) {
            cell = [[SSFirstPageTravelHotCityCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:city];
            cell.delegate = self;
        }
        
        cell.cityArr = self.cityOfSectionArr[0];
        [cell.hotCityCollectionView reloadData];
        
        return cell;
        
    } else {
        
        static NSString *city = @"city";
        SSFirstPageTravelCityCell *cell = [tableView dequeueReusableCellWithIdentifier:city];
        if (!cell) {
            cell = [[SSFirstPageTravelCityCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:city];
        }
        
        SSFirstPageTravelCityModel *cityModel = self.cityOfSectionArr[indexPath.section][indexPath.row];
        
        [cell.photoView sd_setImageWithURL:[NSURL URLWithString:cityModel.photo] placeholderImage:[UIImage imageNamed:@"placeholder1"]];
        cell.cnnameLabel.text = cityModel.cnname;
        cell.ennameLabel.text = cityModel.enname;
        return cell;
    }
}


- (void)clickHotCityCollectionViewCellByItem:(NSInteger)item
{
    SSFirstPageTravelCityModel *cityModel = self.cityOfSectionArr[0][item];
    if (self.ifReturnLastInterface) {
        
        self.block(cityModel);
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        NSLog(@"跳转");
        NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/city_detail?city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88265581038946&lon=121.5397240097761&oauth_token=3ccf6be2c77dee4d33e6d6a5eb878fc7&page=1&track_app_channel=App%%2520Store&track_app_version=6.6&track_device_info=iPhone7%%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%%25208.1.2&track_user_id=6654242&v=1%%20", cityModel.myid];
        
        MRCityViewController *cityViewController = [[MRCityViewController alloc] init];
        cityViewController.url = url;
        [self.navigationController pushViewController:cityViewController animated:YES];

    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SSFirstPageTravelCityModel *cityModel = self.cityOfSectionArr[indexPath.section][indexPath.row];
    
    if (self.ifReturnLastInterface) {
        
        if (indexPath.section > 0) {
            self.block(cityModel);
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    } else {
        NSLog(@"跳转");
        
        NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/city_detail?city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88265581038946&lon=121.5397240097761&oauth_token=3ccf6be2c77dee4d33e6d6a5eb878fc7&page=1&track_app_channel=App%%2520Store&track_app_version=6.6&track_device_info=iPhone7%%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%%25208.1.2&track_user_id=6654242&v=1%%20", cityModel.myid];
        
        MRCityViewController *cityViewController = [[MRCityViewController alloc] init];
        cityViewController.url = url;
        [self.navigationController pushViewController:cityViewController animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
